package com.terrier.ui.controls
{
    import com.terrier.ui.controls.supportClasses.WxSpinner;
    import com.terrier.ui.skins.IWxSkinBase;
    import com.terrier.ui.skins.IWxNumericStepperSkin;

    import flash.events.Event;

    /**
     * 数字增减器
     * @author Jarvis.weng
     */
    public class WxNumericStepper extends WxSpinner
    {
        protected var textDisplay:WxTextInput;

        private var _valueFormatFunction:Function;

        private var _valueParseFunction:Function

        public function WxNumericStepper()
        {
            super();
        }

        override public function set value(newValue:Number):void
        {
            var oldValue:Number = value;
            super.value = newValue;
            applyValueDisplay();

            if (oldValue != value)
            {
//                var event:WxNumericStepperEvent = new WxNumericStepperEvent(WxNumericStepperEvent.VALUE_CHANGE,
//                                                                            value, oldValue);
//                dispatchEvent(event);
            }
        }

        override public function setSkin(value:IWxSkinBase):void
        {
            super.setSkin(value);

            if (skin != null)
            {
                increaseButton.setSkin(skin.increaseButtonSkin);
                decreaseButton.setSkin(skin.decreaseButtonSkin);
                textDisplay.setSkin(skin.textDisplayStyle);
            }

            invalidateView();
            invalidateLayout();
        }

        override protected function constrainProperties(minimumChanged:Boolean, maximumChanged:Boolean, snapIntervalChanged:Boolean):void
        {
            super.constrainProperties(minimumChanged, maximumChanged, snapIntervalChanged);
            applyValueDisplay();
        }

        override protected function createChildren():void
        {
            super.createChildren();

            textDisplay = new WxTextInput;
            addChild(textDisplay);
            textDisplay.textField.addEventListener(Event.CHANGE, endEditHandler);

            applyValueDisplay();
        }

        override protected function updateLayout():void
        {
            if (skin == null || skin.customLayout)
            {
                return;
            }
            textDisplay.width = 50;
            increaseButton.x = textDisplay.x + textDisplay.width;
            increaseButton.y = 0;
            decreaseButton.x = textDisplay.x + textDisplay.width;
            decreaseButton.y = increaseButton.height;
        }

        public function get valueFormatFunction():Function
        {
            return _valueFormatFunction;
        }

        public function set valueFormatFunction(value:Function):void
        {
            if (value == _valueFormatFunction)
            {
                return;
            }

            _valueFormatFunction = value;

            applyValueDisplay();
        }

        public function get valueParseFunction():Function
        {
            return _valueParseFunction;
        }

        public function set valueParseFunction(value:Function):void
        {
            if (value == _valueParseFunction)
            {
                return;
            }

            _valueParseFunction = value;

            parseValueDisplay();
        }

        private function parseValueDisplay():void
        {
            if (valueParseFunction != null)
            {
                value = valueParseFunction(textDisplay.text);
            }
            else
            {
                value = Number(textDisplay.text);
            }
        }

        private function applyValueDisplay():void
        {
            if (valueFormatFunction != null)
            {
                textDisplay.text = valueFormatFunction(value);
            }
            else
            {
                textDisplay.text = value.toString();
            }
        }

        private function endEditHandler(e:Event):void
        {
            parseValueDisplay();
        }

        private function get skin():IWxNumericStepperSkin
        {
            return _skin as IWxNumericStepperSkin;
        }
    }
}
